#!/bin/bash

# Configure Global`s Variables
VERSION="0.0.25"
NAME="SlackConf GUI for Slackware Configuration"
DIA="/bin/dialog"
BKP_OPT="Y"

# Function check_depends
function check_depends()
{
	#Check if user run is 'root'
	USER_RUN=`whoami`
        if [ $USER_RUN != "root" ]; then
                echo -e ''
                echo -e '*** Please only run as root user ***'
                echo -e ''
                echo -e ''
                exit_
        fi

	#Create temporary directories, and set global variable
	mkdir -p /tmp/.slackconf
	DIR_TMP="/tmp/.slackconf"
}

# Function for exit program
function exit_()
{
	rm -rf /tmp/.slackconf
	#Exit program
	echo -e "$NAME"
	echo -e "========================================="
	echo -e ""
	echo -e "bye ... ;-)"
	echo -e ""
	echo -e ""
	exit 0;
}

# Function for display information about program
function about()
{
	$DIA	\
	--title "$NAME - $VERSION"	\
	--msgbox "\n $NAME, version $VERSION.
		-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- \n\n\n
		slackconf is a tool for setting up a Slackware Linux System. \n\n
		You can help by reporting an error, correcting some function, or by \n
		sending ideas in the solution can be useful. \n\n
		Or if you want to submit your comment, please write to: \n\n
		contato@brunorusso.eti.br \n\n\n
		http://brunorusso.eti.br/slackconf \n\n\n
		Slackware (R) is a registered trademark of Patrick Volkerding, for more \n 
		information visit: http://slackware.com/trademark/trademark.php \n 	
		Join the project group: slackconf@googlegroups.com "	\
	30 90
	main_menu
}

#Function Conf_Network
Conf_Network()
{
	#run netconfig
	netconfig
	main
}

#Function Conf_Mouse
Conf_Mouse()
{
	#run mouseconfig
	mouseconfig
	main
}

#Function Conf_Packages
Conf_Packages()
{
	#run pkgtool
	pkgtool
	main
}

#Function Conf_Log
Conf_Log()
{
	LOG_OPT=$($DIA --stdout	\
		--title "$NAME - $VERSION"	\
		--menu 'Do you need?'	\
		0 0 0	\
		Messages 'logs from system'  \
		Security 'messages from system'  \
		Mail 	 'log from system'  \
		Syslog	 'from ssytem' )

        if [ $? = 1 ]; then
                main_menu
        fi

	#View messages
	if [ $LOG_OPT = "Messages" ]; then
		tail -f /var/log/messages > $DIR_TEMP/out &

		$DIA \
		--title "$NAME - $VERSION"	\
		--tailbox $DIR_TEMP/out		\
		0 0
		Conf_Log
	fi
	#View security messages
	if [ $LOG_OPT = "Security" ]; then
		tail -f /var/log/secure > $DIR_TEMP/out &

		$DIA \
		--title "$NAME - $VERSION"	\
		--tailbox $DIR_TEMP/out		\
		0 0
		Conf_Log
	fi
	#View maillog messages
	if [ $LOG_OPT = "Mail" ]; then
		tail -f /var/log/maillog > $DIR_TEMP/out &

		$DIA \
		--title "$NAME - $VERSION"	\
		--tailbox $DIR_TEMP/out		\
		0 0
		Conf_Log
	fi
	#View syslog messages
	if [ $LOG_OPT = "Syslog" ]; then
		tail -f /var/log/syslog > $DIR_TEMP/out &

		$DIA \
		--title "$NAME - $VERSION"	\
		--tailbox $DIR_TEMP/out		\
		0 0
		Conf_Log
	fi
	main_menu
}

#Function Conf_Sound
Conf_Sound()
{
	SOUND_OPT=$($DIA --stdout	\
		--title "$NAME - $VERSION"	\
		--menu 'Do you need?'	\
		0 0 0	\
		Modify 'sound volum'  \
		Configure 'sound card' )

        if [ $? = 1 ]; then
                main_menu
        fi

	#Modify sound volum
	if [ $SOUND_OPT = "Modify" ]; then
		#run alsamixer
		alsamixer
	fi

	#Configure sound card
	if [ $SOUND_OPT = "Configure" ]; then
		#run alsaconf
		alsaconf
	fi
	main_menu
}


#Function Conf_System
Conf_System()
{
	SYSTEM_OPT=$($DIA --stdout	\
		--title "$NAME - $VERSION"	\
		--menu 'Do you need more informatin...'	\
		0 0 0	\
		CPU 'configuration'  \
		Memory 'information' )

        if [ $? = 1 ]; then
                main_menu
        fi

	if [ $SYSTEM_OPT = "CPU" ]; then
		cat /proc/cpuinfo > $DIR_TEMP/cpuinfo
		$DIA \
		--title "$NAME - $VERSION"	\
		--textbox $DIR_TEMP/cpuinfo		\
		0 0
		Conf_System
	fi

	if [ $SYSTEM_OPT = "Memory" ]; then
		echo "Simple information" > $DIR_TEMP/memory
		echo "==================" >> $DIR_TEMP/memory
		echo "" >> $DIR_TEMP/memory
		free >> $DIR_TEMP/memory
		echo "" >> $DIR_TEMP/memory
		echo "" >> $DIR_TEMP/memory
		echo "Full information" >> $DIR_TEMP/memory
		echo "==================" >> $DIR_TEMP/memory
		echo "" >> $DIR_TEMP/memory
		cat /proc/meminfo >> $DIR_TEMP/memory
		$DIA \
		--title "$NAME - $VERSION"	\
		--textbox $DIR_TEMP/memory		\
		0 0
		Conf_System
	fi

	main_menu
}

#Function Check_IP
Check_IP()
{
	#need check is OK!
	VERIFY_IP=`echo $1 | egrep '^([0-9]{1,3}\.){3}[0-9]{1-3}$' -c`
	VERIFY_IP="1"
	if [ $VERIFY_IP -eq "0" ]; then
		$DIA \
		--title "$NAME - $VERSION" \
		--msgbox "Sorry, but \"$1\" dont see a valid IP adrress" \
		0 0
		return 0
	fi
	return 1
}

#Function Conf_Dns
Conf_Dns()
{
	clear
        DNS_CUR=`cat /etc/resolv.conf | grep -v ^# | cut -f2 -d" " `
	DNS_OPTION=$( $DIA \
		--stdout \
		--title "$NAME - $VERSION"	\
		--inputbox "The DNS Servers currently configured are: \n 
			  $DNS_CUR \n\n 
			  Input your host or domain to check! " \
		 0 0 )
		
	#If cancel return is 1	
	if [ $? = 1 ]; then
		main
	fi

	#Check doman
	if [ $DNS_OPTION != " " ]; then
		echo "     $DNS_OPTION" | tr [:lower:] [:upper:] > $DIR_TMP/dns.log
		echo "" >> $DIR_TMP/dns.log
		echo "" >> $DIR_TMP/dns.log
		nslookup $DNS_OPTION >> $DIR_TMP/dns.log
		$DIA \
		--title "$NAME - $VERSION"	\
		--textbox $DIR_TMP/dns.log		\
		0 0
		Conf_Dns
	else
		#return to main 
		main
	fi
}
#Function Conf_User
Conf_User()
{
echo "hello"

}


#Function Conf_Routes
Conf_Routes()
{
	clear
	ROUTES_NOW=`route -n`
	ROUTES_OPTION=$( $DIA \
		--stdout \
		--title "$NAME - $VERSION"	\
		--yesno "The routing table is configured as follows: \n\n $ROUTES_NOW \n\n\n \
		Want to change? \n\n" \
		 0 0 )
		if [ $? = 0 ]; then
			Create_Route
		else
			#return to main 
			main
		fi
}

#Functio Create_Route
Create_Route()
{
	ROUTE_CREATE_OPT=$($DIA --stdout	\
		--title "$NAME - $VERSION"	\
		--menu 'Do you need?'	\
		0 0 0	\
		1 'Create a new Route'	\
		2 'Modify a existing Route'	\
		3 'Delete a existing Route')

        if [ $? = 1 ]; then
                main_menu
        fi

	if [ $ROUTE_CREATE_OPT == "1" ]; then
		ROUTE_TYPE=$($DIA --stdout	\
			--title "$NAME - $VERSION"	\
			--menu 'You will create a route  to a network or a host?'	\
			0 0 0	\
			1 'Network'	\
			2 'Host')

	        if [ $? = 1 ]; then
        	        main_menu
	        fi

		ROUTE_HOST=$( $DIA \
			--stdout \
			--title "$NAME - $VERSION"	\
			--inputbox "Insert HOST or NETWORK destination. \n\n  \
			\nHost --> 192.168.0.1
			\n
			\nNETWORK --> 10.0.0.0 netmask 255.255.0.0 \n
		\n" 0 0 )

		#If cancel return is 1	
		if [ $? = 1 ]; then
			main
		fi

		ROUTE_GW=$( $DIA \
			--stdout \
			--title "$NAME - $VERSION"	\
			--inputbox "Insert ROUTER ADDRESS. \n\n  \
			\nRouter --> 192.168.0.1
		\n" 0 0 )

		#If cancel return is 1	
		if [ $? = 1 ]; then
			main
		fi

		if [ $ROUTE_TYPE == "1" ]; then
			echo "route add -net $ROUTE_HOST gw $ROUTE_GW"
			route add -net $ROUTE_HOST gw $ROUTE_GW
		fi
		if [ $ROUTE_TYPE == "2" ]; then
			echo "route add -host $ROUTE_HOST gw $ROUTE_GW"
			route add -host $ROUTE_HOST gw $ROUTE_GW
		fi

	fi

	if [ $ROUTE_CREATE_OPT == "2" ]; then
		echo "Modify route"
	fi
	
	if [ $ROUTE_CREATE_OPT == "3" ]; then
		echo "Delete route"
	fi
}


#Function BKP_FILE
function BKP_FILE()
{
	cp $1 $1.bkp	
}


#Function Conf_Update
function Conf_Update()
{
	UPDATE_OPT=$($DIA --stdout	\
		--title "$NAME - $VERSION"	\
		--menu 'Do you need?'	\
		0 0 0	\
		Check 	'for available updates'  \
		Install	'new package'	\
		Upgrade	'a package'	\
		Reinstall	'a package'	\
		Remove	'a package'	\
		Search	'a package'	\
		Search-File	'into Pakage'	\
		Info	'about a package'	\
	       	)

        if [ $? = 1 ]; then
                main_menu
        fi

	if [ $UPDATE_OPT = "Check" ]; then
		#run slackpkg update
		slackpkg update
		echo "Done. Return to slackconf!"
		sleep 3
		Conf_Update
	fi
	if [ $UPDATE_OPT = "Install" ]; then
		UPDATE_NAME=$($DIA --stdout	\
			--title "$NAME - $VERSION"	\
			--inputbox "Enter the name of the package to be installed"	\
			0 0 )
		#run slackpkg install
		slackpkg install $UPDATE_NAME
		echo "Done. Return to slackconf!"
		sleep 3 
		Conf_Update
	fi
	if [ $UPDATE_OPT = "Upgrade" ]; then
		UPDATE_NAME=$($DIA --stdout	\
			--title "$NAME - $VERSION"	\
			--inputbox "Enter the name of the package to be upgraded"	\
			0 0 )
		#run slackpkg upgrade
		slackpkg upgrade $UPDATE_NAME
		echo "Done. Return to slackconf!"
		sleep 3 
		Conf_Update
	fi
	if [ $UPDATE_OPT = "Reinstall" ]; then
		UPDATE_NAME=$($DIA --stdout	\
			--title "$NAME - $VERSION"	\
			--inputbox "Enter the name of the package to be re-installed"	\
			0 0 )
		#run slackpkg reinstall
		slackpkg reinstall $UPDATE_NAME
		echo "Done. Return to slackconf!"
		sleep 3 
		Conf_Update
	fi
	if [ $UPDATE_OPT = "Remove" ]; then
		UPDATE_NAME=$($DIA --stdout	\
			--title "$NAME - $VERSION"	\
			--inputbox "Enter the name of the package to be removed"	\
			0 0 )
		#run slackpkg remove
		slackpkg remove $UPDATE_NAME
		echo "Done. Return to slackconf!"
		sleep 3 
		Conf_Update
	fi
	if [ $UPDATE_OPT = "Search" ]; then
		UPDATE_NAME=$($DIA --stdout	\
			--title "$NAME - $VERSION"	\
			--inputbox "Enter the name of the package you want to search"	\
			0 0 )
		#run slackpkg search
		slackpkg search $UPDATE_NAME
		echo "Done. Return to slackconf!"
		sleep 0 
		Conf_Update
	fi
	if [ $UPDATE_OPT = "Search-File" ]; then
		UPDATE_NAME=$($DIA --stdout	\
			--title "$NAME - $VERSION"	\
			--inputbox "Enter the name of the file you want to find"	\
			0 0 )
		#run slackpkg file-search
		slackpkg file-search $UPDATE_NAME
		echo "Done. Return to slackconf!"
		sleep 3 
		Conf_Update
	fi
	if [ $UPDATE_OPT = "Info" ]; then
		UPDATE_NAME=$($DIA --stdout	\
			--title "$NAME - $VERSION"	\
			--inputbox "Enter the name of the package you want information"	\
			0 0 )
		#run slackpkg info
		slackpkg info $UPDATE_NAME
		echo "Done. Return to slackconf!"
		sleep 3 
		Conf_Update
	fi
}


#Function Conf_Runlevel
function Conf_Runlevel()
{
	FILE_INITTAB="/etc/inittab"
	#Detect runlevel configurated
	RUNLEVEL=`cat /etc/inittab | grep id: | cut -d":" -f2`
	RUNLEVEL_OPTION=$( $DIA \
		--stdout \
		--title "$NAME - $VERSION"	\
		--inputbox "Your level configured is $RUNLEVEL, but you can change. \n\n \
		\n0	halt
		\n1	single user mode
		\n2	unused (but configured the same as runlevel 3)
		\n3	multiuser mode (default Slackware runlevel)
		\n4	X11 with KDM/GDM/XDM (session managers)
		\n5	unused (but configured the same as runlevel 3)
		\n6	reboot \n" 0 0 )

	#If cancel return is 1	
	if [ $? = 1 ]; then
		main
	fi

	#if opntion is equal runlevel, nothing
	if [ $RUNLEVEL_OPTION == $RUNLEVEL ]; then
		main;
	fi

	#Case value not 0-6 reload this function
	if [[ $RUNLEVEL_OPTION != [0-6] ]]; then
		Conf_Runlevel
	fi

	if [[ $RUNLEVEL_OPTION = [0-6] ]]; then
		RUNLEVEL_OPTION_YN=$($DIA	\
			--stdout	\
			--title "$NAME - $VERSION"	\
			--yesno "Runlevel is changed to $RUNLEVEL_OPTION, ok?"	\
			0 0	\
		)
		if [ $? = 0 ]; then
			#change file
			if [ $BKP_OPT == "Y" ]; then
				BKP_FILE "$FILE_INITTAB"
			fi
			sed "s/id:$RUNLEVEL:initdefault:/id:$RUNLEVEL_OPTION:initdefault:/g" $FILE_INITTAB > /tmp/INITTAB.tmp && cp /tmp/INITTAB.tmp $FILE_INITTAB && rm /tmp/INITTAB.tmp
			main
		else
			#return to conf_Runlevel 
			Conf_Runlevel
		fi
	fi

}

#Function Conf_Services
function Conf_Services()
{
	SERVICE_OPT=$($DIA --stdout	\
		--title "$NAME - $VERSION"	\
		--menu 'Do you need?'	\
		0 0 0	\
		Enables	'a Service'	\
		Disables 'a Service')

        if [ $? = 1 ]; then
                main_menu
        fi
	if [ $SERVICE_OPT == "Disables" ]; then
		#Identifies the executable scripts
		find /etc/rc.d/ -perm -400 | grep -v "rc.[0-6]" | grep -v "rc.[K,M,S]" | grep -v "init.d" | grep -v "rc[0-9]" | cut -d"/" -f4 | sort > /tmp/.services.slackconf00
		cp /tmp/.services.slackconf00 /tmp/.services.slackconf01
		paste /tmp/.services.slackconf00 /tmp/.services.slackconf01 -d" " > /tmp/.services.slackconf
		#cat /tmp/.services.slackconf02 | sed 's/$/ ON/' > /tmp/.services.slackconf
		escolha=$($DIA --stdout	\
			--title "$NAME - $VERSION"	\
			--menu "What service you want to disable?"	\
			0 0 0 	\
			$(cat /tmp/.services.slackconf))
		chmod -x /etc/rc.d/$escolha
		Conf_Services
	fi
	#Identifies non executable scripts
	find /etc/rc.d/ -perm -400 | grep -v "rc.[0-6]" | grep -v "rc.[K,M,S]" | grep -v "init.d" | grep -v "rc[0-9]" | cut -d"/" -f4 | sort > /tmp/.services.slackconf00
	cp /tmp/.services.slackconf00 /tmp/.services.slackconf01
	paste /tmp/.services.slackconf00 /tmp/.services.slackconf01 -d" " > /tmp/.services.slackconf
	#cat /tmp/.services.slackconf02 | sed 's/$/ ON/' > /tmp/.services.slackconf
	escolha=$($DIA --stdout	\
		--title "$NAME - $VERSION"	\
		--menu "What service you want to enable?"	\
		0 0 0 	\
		$(cat /tmp/.services.slackconf))
	chmod +x /etc/rc.d/$escolha
	Conf_Services
}

main_menu()
{
	OPTION=$($DIA --stdout  \
		--title "$NAME - $VERSION"      \
		--menu 'Which option would you like?'   \
		0 0 0                                     \
		RunLevel     'Configure runlevel startup' \
		Packages     'Manage Packages using pkgtool'      \
		Update       'using slackpkg'      \
		Services     'Enable or disable startup script'      \
		Mouse	     'Configure mouse using mouseconfig'      \
		Network      'Configure network using netconfig'      \
		DNS	     'Query information about host/domain'      \
		Routes	     'Display and configure the routing table in the kernel'      \
		Sound	     'Configure sound devices'      \
		Log	     'View logs from system'      \
		User	     'Management users account'      \
		System	     'Information'      \
		About        "About $NAME"      \
	Exit         "Return to prompt")


        if [ $? = 1 ]; then
                exit_
        fi
        if [ $OPTION == 'RunLevel' ]; then
                Conf_Runlevel
        elif [ $OPTION == "Services" ]; then
		main_menu
                #Conf_Services
        elif [ $OPTION == "Packages" ]; then
                Conf_Packages
        elif [ $OPTION == "Network" ]; then
                Conf_Network
        elif [ $OPTION == "Mouse" ]; then
                Conf_Mouse
        elif [ $OPTION == "About" ]; then
                about
        elif [ $OPTION == "DNS" ]; then
                #main_menu
		Conf_Dns
        elif [ $OPTION == "Routes" ]; then
		main_menu
                #Conf_Routes
        elif [ $OPTION == "Sound" ]; then
                Conf_Sound
        elif [ $OPTION == "Log" ]; then
                Conf_Log
        elif [ $OPTION == "System" ]; then
                Conf_Sound
        elif [ $OPTION == "Update" ]; then
                Conf_Update
        elif [ $OPTION == "User" ]; then
		main_menu
                #Conf_User
        elif [ $OPTION == "Exit" ]; then
                exit_
        fi

}

main()
{
	check_depends
	main_menu
}
main
